# -*- coding: utf-8 -*-
# 作者    ：SunDuWei
# 创作时间 ：2020-02-24
# 使用期货数据导入做初步的机器学习
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
import numpy as np
# 数据导入


class Dataprocess():

    @classmethod
    def readdata(cls, path):
        df = pd.read_excel(path, parse_dates=True)

        return df

    @classmethod
    def data_feature_engineering(cls, df):
        df.dropna(axis=0, how='any', inplace=True)

        def isbreak(x):
            if x > +15:
                return 1
            elif x < -15:
                return -1
        df['y1_long_short_break'] = df.y1_30min_yield.apply(lambda x: isbreak(x))
        df.y1_long_short_break.fillna(0, inplace=True)
        return df

    @classmethod
    def datastandlize(cls, x1):
        sc = StandardScaler()
        sc.fit(x1)
        x1_std = sc.transform(x1)
        return x1_std

    @classmethod
    def datasplit(cls, df):
        ser_col = pd.Series(df.columns)
        y_col = list(ser_col[ser_col.str.startswith("y1")])
        txt_col = list(df.dtypes[df.dtypes == 'object'].index)
        non_x_col = txt_col + y_col + ['datetime'] + ['time']
        x_col = list(set(ser_col.values).difference(non_x_col))
        x = df.loc[:, x_col]
        y = df['y1_long_short_break']
        x_train, x_non_train, y_train, y_non_train = train_test_split(x, y, test_size=0.2, random_state=0)
        x_val, x_test, y_val, y_test = train_test_split(x_non_train, y_non_train, test_size=0.2, random_state=0)
        x_train_std = Dataprocess.datastandlize(x_train)
        return x_test, x_train_std, x_val, y_test, y_train, y_val


if __name__ == "__main__":
    path = r'E:\课堂资料\future_data\factor_sheet.xlsx'
    df = Dataprocess().readdata(path)
    df = Dataprocess().data_feature_engineering(df)
    x_test, x_train_std, x_val, y_test, y_train, y_val = Dataprocess().datasplit(df)
    print(np.isnan(x_train_std).any(), np.isnan(y_train).any())
    print(np.isfinite(x_train_std).all(), np.isfinite(y_train).all())
    lr = LogisticRegression(C=1000, random_state=0)
    lr.fit(x_train_std, y_train)
    param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100]}
    # 交差验证与网格调参
    grid_search = GridSearchCV(LogisticRegression(), param_grid=param_grid, cv=2)
    grid_search.fit(x_train_std, y_train)
    grid_search.score(x_test, y_test)
    print(grid_search.best_params_)
    print(grid_search.best_score_)
    print(grid_search.best_estimator_)

